package top.yiwyn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.yiwyn.common.ResultBean;
import top.yiwyn.domain.Arcticle;
import top.yiwyn.domain.PageData;
import top.yiwyn.domain.Tag;
import top.yiwyn.mapper.ArcticleTagMapper;
import top.yiwyn.mapper.LoggerMapper;
import top.yiwyn.mapper.TagMapper;


import java.util.List;

@RestController
@RequestMapping("/tag")
public class TagController {

    @Autowired
    TagMapper tagMapper;

    @Autowired
    ArcticleTagMapper arcticleTagMapper;


    /**
     * 添加标签，String返回状态，后期修改为封装类统一返回
     *
     * @param tagName
     * @return
     */
    @PostMapping("/addtag")
    public ResultBean tagAdd(String tagName) {
        List<Tag> tagList = tagMapper.selectAll();

        for (Tag tag : tagList) {
            if (tag.getTagName().equals(tagName)) {
                return ResultBean.fail("已经存在");
            }
        }
        tagMapper.insert(new Tag(tagName));
        return ResultBean.succ("添加成功", null);
    }

    @DeleteMapping("/deletetag/{id}")
    public ResultBean tagDelete(@PathVariable("id") long id) {
        tagMapper.deleteById(id);
        arcticleTagMapper.deleteById(id);
        return ResultBean.succ("删除成功", null);
    }

    @PostMapping("/modify/{id}")
    public ResultBean modify(Tag tag) {

        tagMapper.updateById(tag);
        return ResultBean.succ("修改成功", null);
    }

    @PostMapping("/updateTag")
    public ResultBean updateTag(Tag tag) {
        if (tag.getId() != -1) {
            tagMapper.updateById(tag);
            return ResultBean.succ("修改成功", null);
        }
        List<Tag> tagList = tagMapper.selectAll();

        for (Tag t : tagList) {
            if (t.getTagName().equals(tag.getTagName())) {
                return ResultBean.fail("已经存在");
            }
        }
        tagMapper.insert(new Tag(tag.getTagName()));

        return ResultBean.succ("添加成功", null);
    }


//    @GetMapping("/alltag")
//    public List<Tag> getAllTag() {
//        List<Tag> tagList = tagMapper.selectAll();
//        return tagList;
//    }

    @GetMapping("/alltag")
    public ResultBean<List<Tag>> getAllTag() {
        List<Tag> tagList = tagMapper.selectAll();

        return ResultBean.succ(tagList);
    }

//    /**
//     * 根据tagId查询所有包含该id的文章
//     *
//     * @param id
//     * @return
//     */
//    @GetMapping("/getarcticlebytag/{id}")
//    public List<Arcticle> selectArcticleByTagId(@PathVariable("id") int id) {
//        List<Arcticle> arcticles = tagMapper.selectArcticleByTagId(id);
//        return arcticles;
//    }


    /**
     * 分页每9个一页
     *
     * @param id
     * @return
     */
    @GetMapping("/gettagpagedata/{id}")
    public PageData getTagPageData(@PathVariable("id") int id) {
        int arcticles = tagMapper.getTagCount(id);
        int pageCount = (arcticles + 9 - 1) / 9;
        return new PageData(pageCount, arcticles);
    }

    /**
     * @param id
     * @param current 当前页 为了符合正常人使用，current-1 输入时从1开始作为第一页
     * @return 说明0参数  前端获取0页使得页面刷新，在后端对参数0进行++操作
     */
    @GetMapping("/getArcticlePage/{id}")
    public List<Arcticle> getArcticlePage(@PathVariable("id") int id, int current) {
        if (current == 0) {
            current++;
        }
        List<Arcticle> records = tagMapper.selectArcticleByTagId(id, (current - 1) * 9, 9);

        return records;
    }


}
